import {
  Injectable,
  NestInterceptor,
  CallHandler,
  ExecutionContext,
} from '@nestjs/common';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { Model } from 'mongoose';

// response
interface Response<T> {
  data: T;
}
const handleResponseData = <T>(value: T) => {
  return { data: value, code: 0, message: '请求成功' };
};

@Injectable()
export class TransformHttpInterceptor<T>
  implements NestInterceptor<T, Response<T>> {
  intercept(
    context: ExecutionContext,
    next: CallHandler<T>,
  ): Observable<Response<T>> {
    return next.handle().pipe(map(data => handleResponseData(data)));
  }
}
